/**
 * 
 */
package com.zhongkai.service.ticket;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import sun.io.Converters;

import com.sun.org.apache.commons.beanutils.BeanUtils;
import com.zhongkai.dao.ticket.TicketDistributingDAO;
import com.zhongkai.model.book.TCsCclxdz;
import com.zhongkai.model.ticket.TPzJc;
import com.zhongkai.model.ticket.TPzYwls;
import com.zhongkai.service.BaseService;

/**
 * @packagename com.zhongkai.service.ticket
 * @filename TicketDistributingService.java
 * @作者 吴嘉俊
 * @创建日期 Aug 8, 2011 2:21:54 PM
 *
 */
@Transactional
@Component
public class TicketDistributingService extends BaseService {
	private TicketDistributingDAO ticketDistributingDAO;
	@Resource
	public void setTicketDistributingDAO(TicketDistributingDAO ticketDistributingDAO) {
		this.ticketDistributingDAO = ticketDistributingDAO;
	}
	
	/**
	 * 
	 * @param yprDm
	 * @return
	 * @throws Exception
	 */
	public List FindTicketsInfoByUser(String yprDm) throws Exception {
		return ticketDistributingDAO.select("from TPzJc jc,TDmCcsPzzl zl,TDmCcsPzzt zt where jc.pzzlDm = zl.pzzlDm and jc.pzztDm = zt.pzztDm and jc.yprDm = ? order by jc.zb, jc.sphmQ", new Object[]{yprDm});
		//return ticketDistributingDAO.select("from TPzJc jc where jc.yprDm = ?", new Object[]{yprDm});
	}
	
	/**
	 * 
	 * @param pzzlDm
	 * @return
	 * @throws Exception
	 */
	public List FindTicketsInfoByType(String pzzlDm) throws Exception {
		return ticketDistributingDAO.select("from TPzJc where pzzlDm = ? and ypdwDm != '' and (yprDm = null or yprDm = '')", new Object[]{pzzlDm});
	}
	
	
	public void SaveAndUpdateTicketNum(String[] yprDm,String admin,String[] pzzlDm,String[] zb,String[] sphmQ,String[] sphmZ,String[] sl) throws Exception{
		Date now = new Date(); 
		for(int i=0;i<pzzlDm.length;i++){
			TPzJc pzJc = new TPzJc();
			pzJc.setPzzlDm(pzzlDm[i]);
			pzJc.setZb(zb[i]);
			pzJc.setSphmQ(sphmQ[i]);
			pzJc.setSphmZ(sphmZ[i]);
			//"01"为空白
			pzJc.setPzztDm("01");
			pzJc.setYprDm(yprDm[i]);
			pzJc.setCzrDm(admin);
			pzJc.setCzRq(now);
			pzJc.setXgrDm(admin);
			pzJc.setXgRq(now);
			
			TPzJc TPzJcFind = FindTicket(pzJc);
			pzJc.setYpdwDm(TPzJcFind.getYpdwDm());
			
			TPzYwls tPzYwls = new TPzYwls();
			BeanUtils.copyProperties(tPzYwls,pzJc);
			tPzYwls.setSl(Integer.parseInt(sl[i]));
			//"26"为代征员领用
			tPzYwls.setYwlxDm("26");
			
			TPzJc pzJcMyEndNumber = FindMyTicketEndNumber(pzJc);
			TPzJc pzJcMyStartNumber = FindMyTicketStartNumber(pzJc);
			
			if(pzJcMyEndNumber == null && pzJcMyStartNumber == null && Integer.parseInt(TPzJcFind.getSphmQ()) == Integer.parseInt(sphmQ[i])){
				ticketDistributingDAO.insert(pzJc);
				
				TPzJcFind.setSphmQ(AddZero(Integer.parseInt(sphmZ[i])+1));
				ticketDistributingDAO.update(TPzJcFind);
			}else if(pzJcMyEndNumber == null && pzJcMyStartNumber == null && Integer.parseInt(TPzJcFind.getSphmZ()) == Integer.parseInt(sphmZ[i])){
				ticketDistributingDAO.insert(pzJc);
				
				TPzJcFind.setSphmZ(AddZero(Integer.parseInt(sphmQ[i])-1));
				ticketDistributingDAO.update(TPzJcFind);
			}else if(pzJcMyEndNumber == null && pzJcMyStartNumber == null && Integer.parseInt(TPzJcFind.getSphmQ()) != Integer.parseInt(sphmQ[i]) && Integer.parseInt(TPzJcFind.getSphmZ()) != Integer.parseInt(sphmZ[i])){
				ticketDistributingDAO.insert(pzJc);

				pzJc = new TPzJc();
				BeanUtils.copyProperties(pzJc,TPzJcFind);
				
				pzJc.setSphmZ(AddZero(Integer.parseInt(sphmQ[i])-1));
				ticketDistributingDAO.insert(pzJc);
				
				TPzJcFind.setSphmQ(AddZero(Integer.parseInt(sphmZ[i])+1));
				ticketDistributingDAO.update(TPzJcFind);
			}else if(pzJcMyEndNumber != null && pzJcMyStartNumber != null){
				pzJcMyEndNumber.setSphmZ(pzJcMyStartNumber.getSphmZ());
				ticketDistributingDAO.update(pzJcMyEndNumber);
				ticketDistributingDAO.delete(pzJcMyStartNumber);
				ticketDistributingDAO.delete(TPzJcFind);
			}else if (pzJcMyEndNumber != null){
				pzJcMyEndNumber.setSphmZ(sphmZ[i]);
				ticketDistributingDAO.update(pzJcMyEndNumber);
				
				TPzJcFind.setSphmQ(AddZero(Integer.parseInt(sphmZ[i])+1));
				if(Integer.parseInt(TPzJcFind.getSphmQ()) > Integer.parseInt(TPzJcFind.getSphmZ())){
					ticketDistributingDAO.delete(TPzJcFind);
				}else{
					ticketDistributingDAO.update(TPzJcFind);
				}
			}else if (pzJcMyStartNumber != null){
				pzJcMyStartNumber.setSphmQ(sphmQ[i]);
				ticketDistributingDAO.update(pzJcMyStartNumber);
				
				TPzJcFind.setSphmZ(AddZero(Integer.parseInt(sphmQ[i])-1));
				if(Integer.parseInt(TPzJcFind.getSphmQ()) > Integer.parseInt(TPzJcFind.getSphmZ())){
					ticketDistributingDAO.delete(TPzJcFind);
				}else {
					ticketDistributingDAO.update(TPzJcFind);
				}
			}
			
			ticketDistributingDAO.insert(tPzYwls);
			
			/*if(Integer.parseInt(TPzJcFind.getSphmQ()) < Integer.parseInt(sphmQ[i])){
				pzJc = new TPzJc();
				BeanUtils.copyProperties(pzJc,TPzJcFind);
				
				pzJc.setSphmZ(AddZero(Integer.parseInt(sphmQ[i])-1));
				ticketDistributingDAO.insert(pzJc);
				
				TPzJcFind.setSphmQ(AddZero(Integer.parseInt(sphmZ[i])+1));
				ticketDistributingDAO.update(TPzJcFind);
			}else if(Integer.parseInt(TPzJcFind.getSphmQ()) == Integer.parseInt(sphmQ[i]) && Integer.parseInt(TPzJcFind.getSphmZ()) < Integer.parseInt(sphmZ[i])){
				TPzJcFind.setSphmQ(AddZero(Integer.parseInt(sphmZ[i])+1));
				ticketDistributingDAO.update(TPzJcFind);
			}else if(Integer.parseInt(TPzJcFind.getSphmQ()) == Integer.parseInt(sphmQ[i]) && Integer.parseInt(TPzJcFind.getSphmZ()) == Integer.parseInt(sphmZ[i])){
				ticketDistributingDAO.delete(TPzJcFind);
			}*/
		}
	}

	/**
	 * @param pzJc
	 * @return
	 * @throws Exception 
	 */
	private TPzJc FindMyTicketEndNumber(TPzJc pzJc) throws Exception {
		String MyTicketEndNumber = AddZero(Integer.parseInt(pzJc.getSphmQ()) - 1);
		List list = ticketDistributingDAO.select("from TPzJc where (ypdwDm != null or ypdwDm != '') and yprDm = ? and pzzlDm = ? and zb = ? and sphmZ = ?", 
				new Object[]{pzJc.getYprDm(),pzJc.getPzzlDm(),pzJc.getZb(),MyTicketEndNumber});
		if(list.size()>0){
			return (TPzJc) list.get(0);
		}else{
			return null;
		}
	}

	/**
	 * @param pzJc
	 * @return
	 * @throws Exception 
	 */
	private TPzJc FindMyTicketStartNumber(TPzJc pzJc) throws Exception {
		String MyTicketStartNumber = AddZero(Integer.parseInt(pzJc.getSphmZ()) + 1);
		List list = ticketDistributingDAO.select("from TPzJc where (ypdwDm != null or ypdwDm != '') and yprDm = ? and pzzlDm = ? and zb = ? and sphmQ = ?", 
				new Object[]{pzJc.getYprDm(),pzJc.getPzzlDm(),pzJc.getZb(),MyTicketStartNumber});
		if(list.size()>0){
			return (TPzJc) list.get(0);
		}else{
			return null;
		}
	}

	/**
	 * @param pzJc
	 * @return
	 * @throws Exception 
	 */
	private TPzJc FindTicket(TPzJc pzJc) throws Exception {
		List list = ticketDistributingDAO.select("from TPzJc where (yprDm = null or yprDm = '') and pzzlDm = ? and zb = ? and (sphmQ <= ? and sphmZ >= ?) and (sphmQ <= ? and sphmZ >= ?)", 
				new Object[]{pzJc.getPzzlDm(),pzJc.getZb(),pzJc.getSphmQ(),pzJc.getSphmQ(),pzJc.getSphmZ(),pzJc.getSphmZ()});
		if(list.size()>0){
			return (TPzJc) list.get(0);
		}else{
			throw new Exception("冇(字别:"+pzJc.getZb()+",起始号码:"+pzJc.getSphmQ()+",终止号码:"+pzJc.getSphmZ()+")呢段票");
		}
	}
	
	/**
	 * 
	 * @param TicketNumber
	 * @return
	 * @throws Exception
	 */
	public String AddZero(int TicketNumber) throws Exception {
		int TicketNumberLength = 4;
		if((TicketNumber+"").length() < TicketNumberLength){
			String zero = "";
			int i = TicketNumberLength - (TicketNumber+"").length();
			for(int j=0;j<i;j++){
				zero += "0";
			}
			return zero + TicketNumber;
		}else{
			return TicketNumber + "";
		}
	}
}
